//
//  ViewController1.m
//  CAShapLayerTest
//
//  Created by Housir on 2018/1/31.
//  Copyright © 2018年 侯玉坤. All rights reserved.
//

#import "ViewController1.h"

#define kDefaultFrame CGRectMake((self.view.frame.size.width - 100)/2, 100, 100, 100)
#define kRoundFrame CGRectMake((self.view.frame.size.width - 100 - 9)/2, 100+9, 82, 82)
@interface ViewController1 ()

@end

@implementation ViewController1

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 60)/2, self.view.frame.size.height - 100, 60, 30)];
    [button setTitle:@"Button" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

-(void)buttonOnClick:(UIButton *)button
{
    button.enabled = NO;
    if ([self.title isEqualToString:@"实心矩形"]) {
        [self drawSolidRectangular];
    }else if ([self.title isEqualToString:@"空心矩形"]){
        [self drawHollowRectangular];
    }else if ([self.title isEqualToString:@"圆角图形"]){
        [self drawRoundedCornerRadiusShape];
    }else if ([self.title isEqualToString:@"扇形"]){
        [self drawFanshaped];
    }else if ([self.title isEqualToString:@"圆"]){
        [self drawRound];
    }else if ([self.title isEqualToString:@"右上圆角"]){
        [self drawByRoundingCornersWithTopRight];
    }else if ([self.title isEqualToString:@"椭圆"]){
        [self drawOval];
    }else if ([self.title isEqualToString:@"曲线"]){
        [self drawCurve];
    }else if ([self.title isEqualToString:@"S型曲线"]){
        [self drawSshapeCurve];
    }else if ([self.title isEqualToString:@"动画绘制S型曲线"]){
        [self drawCurveWithAnimation];
    }else if ([self.title isEqualToString:@"中间向两边画"]){
        [self drawFromMiddleToBorder];
    }else if ([self.title isEqualToString:@"动画绘制曲线 线边宽"]){
        [self drawCurveWithAnimation3];
    }else if ([self.title isEqualToString:@"动画画圆"]){
        [self drawRoundWithAnimation];
    }else if ([self.title isEqualToString:@"时光网APP 购票layer"]){
        [self draw];
    }else if ([self.title isEqualToString:@"画饼图"]){
        [self drawPie];
    }else if ([self.title isEqualToString:@"动画画空心矩形"]){
        [self drawHollowRectangularWithAnimation];
    }else if ([self.title isEqualToString:@"动画饼图"]){
        [self drawPieWithAnimation];
    }else if ([self.title isEqualToString:@"五角星"]){
        [self drawFivePointedStar];
    }
}
- (void)drawFivePointedStar
{
    CGPoint center = self.view.center;
    CGFloat radius = (self.view.frame.size.width - 100)/2;
    NSDictionary *dic = [self calculateFivePointedStarTenPointWithCenter:center andRadius:radius];
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    basicAnimation.fromValue = @0.0;
    basicAnimation.toValue = @1.0;
    basicAnimation.duration = 10;
    CABasicAnimation *basicAnimation2 = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
    basicAnimation2.fromValue = @0.0;
    basicAnimation2.toValue = @1.0;
    basicAnimation2.duration = 10;
    basicAnimation2.beginTime = 10;
    CAAnimationGroup *groupAnimation = [[CAAnimationGroup alloc]init];
    groupAnimation.animations = @[basicAnimation,basicAnimation2];
    groupAnimation.duration = 20.0;
    groupAnimation.repeatCount = MAXFLOAT;
    
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    NSArray *outerPoints = dic[@"outer"];
    NSArray *interiorPoints = dic[@"interior"];
    CGPoint outerP0 = [outerPoints[0][@"outerP"] CGPointValue];
    CGPoint outerP1 = [outerPoints[1][@"outerP"] CGPointValue];
    CGPoint outerP2 = [outerPoints[2][@"outerP"] CGPointValue];
    CGPoint outerP3 = [outerPoints[3][@"outerP"] CGPointValue];
    CGPoint outerP4 = [outerPoints[4][@"outerP"] CGPointValue];
    CGPoint interiorP0 = [interiorPoints[0][@"interiorP"] CGPointValue];
    CGPoint interiorP1 = [interiorPoints[1][@"interiorP"] CGPointValue];
    CGPoint interiorP2 = [interiorPoints[2][@"interiorP"] CGPointValue];
    CGPoint interiorP3 = [interiorPoints[3][@"interiorP"] CGPointValue];
    CGPoint interiorP4 = [interiorPoints[4][@"interiorP"] CGPointValue];
    [path moveToPoint:outerP0];
    [path addLineToPoint:interiorP3];
    [path addLineToPoint:interiorP4];
    [path addLineToPoint:outerP2];
    [path addLineToPoint:interiorP0];
    [path addLineToPoint:interiorP1];
    [path addLineToPoint:outerP4];
    [path addLineToPoint:interiorP2];
    [path addLineToPoint:interiorP3];
    [path addLineToPoint:outerP1];
    [path addLineToPoint:interiorP4];
    [path addLineToPoint:interiorP0];
    [path addLineToPoint:outerP3];
    [path addLineToPoint:interiorP1];
    [path addLineToPoint:interiorP2];
    [path closePath];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor redColor].CGColor;
    [layer addAnimation:groupAnimation forKey:nil];
    [self.view.layer addSublayer:layer];
}
- (NSDictionary *)calculateFivePointedStarTenPointWithCenter:(CGPoint)center andRadius:(CGFloat)radius
{
    //按顺时针,第一个点在正上方
    //外层五个点
    CGPoint outerP0 = CGPointMake(center.x, center.y-radius);
    CGPoint outerP1 = CGPointMake(center.x+cos(18.0/180.0*M_PI)*radius, center.y-sin(18.0/180.0*M_PI)*radius);
    CGPoint outerP2 =CGPointMake(center.x+cos(54.0/180.0*M_PI)*radius, center.y+sin(54.0/180.0*M_PI)*radius);
    CGPoint outerP3 = CGPointMake(center.x-cos(54.0/180.0*M_PI)*radius, center.y+sin(54.0/180.0*M_PI)*radius);
    CGPoint outerP4 = CGPointMake(center.x-cos(18.0/180.0*M_PI)*radius, center.y-sin(18.0/180.0*M_PI)*radius);
    
    //内层半径
    CGFloat interiorRadius = (1 + pow(tan(18.0/180.0*M_PI), 2))/(3- pow(tan(18.0/180.0*M_PI), 2)) * radius;
    
    //内层五个点
    CGPoint interiorP0 = CGPointMake(center.x, center.y+interiorRadius);
    CGPoint interiorP1 = CGPointMake(center.x-cos(18.0/180.0*M_PI)*interiorRadius, center.y+sin(18.0/180.0*M_PI)*interiorRadius);
    CGPoint interiorP2 =CGPointMake(center.x-cos(54.0/180.0*M_PI)*interiorRadius, center.y-sin(54.0/180.0*M_PI)*interiorRadius);
    CGPoint interiorP3 = CGPointMake(center.x+cos(54.0/180.0*M_PI)*interiorRadius, center.y-sin(54.0/180.0*M_PI)*interiorRadius);
    CGPoint interiorP4 = CGPointMake(center.x+cos(18.0/180.0*M_PI)*interiorRadius, center.y+sin(18.0/180.0*M_PI)*interiorRadius);
    
    
    
    NSMutableDictionary *muDic = [[NSMutableDictionary alloc]init];
    NSArray *outer = @[
                       @{@"outerP":[NSValue valueWithCGPoint:outerP0]},
                       @{@"outerP":[NSValue valueWithCGPoint:outerP1]},
                       @{@"outerP":[NSValue valueWithCGPoint:outerP2]},
                       @{@"outerP":[NSValue valueWithCGPoint:outerP3]},
                       @{@"outerP":[NSValue valueWithCGPoint:outerP4]}
                       ];
    [muDic setObject:outer forKey:@"outer"];
    
    NSArray *interior = @[
                          @{@"interiorP":[NSValue valueWithCGPoint:interiorP0]},
                          @{@"interiorP":[NSValue valueWithCGPoint:interiorP1]},
                          @{@"interiorP":[NSValue valueWithCGPoint:interiorP2]},
                          @{@"interiorP":[NSValue valueWithCGPoint:interiorP3]},
                          @{@"interiorP":[NSValue valueWithCGPoint:interiorP4]}
                          ];
    [muDic setObject:interior forKey:@"interior"];
    
    return muDic;
}
/** 动画饼图 */
/**
 *  这种动画是利用一个layer覆盖在已经画好的饼图上面,然后对覆盖的layer'strokeEnd'做动画
 *  在自定义UIView里面使用self.bouns
 *  画图时候需要center这个center是这个UIView的中心点,而不是它在父容器的center
 */
- (void)drawPieWithAnimation {
    //* 最外层layer */
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:kDefaultFrame cornerRadius:kDefaultFrame.size.height/2];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.strokeColor = [UIColor clearColor].CGColor;
    [self.view.layer addSublayer:layer];
    
    NSArray *sections = @[@20,@10,@40,@60,@70];
    NSArray *sectionColors = @[[UIColor lightGrayColor],[UIColor yellowColor],[UIColor cyanColor],[UIColor orangeColor],[UIColor purpleColor]];
    float total = 0.0,starAngle=M_PI/2,endAngle;
    for (NSInteger i=0; i<sections.count; i++) {
        total += [sections[i] floatValue];
    }
    CGPoint center = CGPointMake(kDefaultFrame.origin.x + kDefaultFrame.size.width/2,kDefaultFrame.origin.y + kDefaultFrame.size.height/2);
    for (NSInteger j=0; j<sections.count; j++) {
        /** 计算比例 */
        CGFloat scale = [sections[j] floatValue]/total;
        UIColor *color = sectionColors[j];
        endAngle = starAngle + scale * 2 * M_PI;
        //* 弧形layer */
        UIBezierPath *fanshapedPath = [UIBezierPath bezierPathWithArcCenter:center radius:(kDefaultFrame.size.height-10)/2 startAngle:starAngle endAngle:endAngle clockwise:YES];
        [fanshapedPath addLineToPoint:center];
        [fanshapedPath closePath];
        CAShapeLayer *fanshapedLayer = [CAShapeLayer layer];
        fanshapedLayer.path = fanshapedPath.CGPath;
        fanshapedLayer.fillColor = color.CGColor;
        /** 重新设置起始位置 */
        starAngle = endAngle;
        [layer addSublayer:fanshapedLayer];
    }
    
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    baseAnimation.fromValue = @1.0;
    baseAnimation.toValue = @0.0;
    baseAnimation.duration = 2.0f;
    /** 逆时针的角度是负的*/
    UIBezierPath *coverPath = [UIBezierPath bezierPathWithArcCenter:center radius:kRoundFrame.size.height/2 startAngle:M_PI_2 endAngle: -2*M_PI+M_PI_2 clockwise:NO];
    CAShapeLayer *coverLayer = [CAShapeLayer layer];
    coverLayer.path = coverPath.CGPath;
    coverLayer.lineWidth = 10;
    coverLayer.fillColor = [UIColor whiteColor].CGColor;
    coverLayer.strokeColor = [UIColor whiteColor].CGColor;
    /** 保持动画执行的状态 */
    baseAnimation.removedOnCompletion = NO;//执行完成后动画对象不要移除
    baseAnimation.fillMode = kCAFillModeForwards;//保持当前状态
    [layer addSublayer:coverLayer];
    [coverLayer addAnimation:baseAnimation forKey:nil];
//    self.layer = coverLayer;
}
/** 动画画空心矩形 */
- (void)drawHollowRectangularWithAnimation {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.fromValue = @0.0;
    animation.toValue = @1.0;
    animation.duration = 2.0f;
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:kDefaultFrame];
    CAShapeLayer *layer = [CAShapeLayer layer];
    //* 设置路径 */
    layer.path = path.CGPath;
    //* 设置填充色 */
    layer.fillColor = [UIColor whiteColor].CGColor;
    //* 设置边框色 */
    layer.strokeColor = [UIColor blackColor].CGColor;
    [layer addAnimation:animation forKey:nil];
    [self.view.layer addSublayer:layer];
}
//画饼图
- (void)drawPie
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:kDefaultFrame cornerRadius:kDefaultFrame.size.width/2];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.strokeColor = [UIColor clearColor].CGColor;
    [self.view.layer addSublayer:layer];
    
    NSArray *sections = @[@20,@10,@40,@60,@70];
    NSArray *sectionColors = @[[UIColor lightGrayColor],[UIColor yellowColor],[UIColor orangeColor],[UIColor purpleColor],[UIColor cyanColor]];
    float total = 0.0,starAngle = 0.0,endAngle;
    for (NSInteger i = 0; i < sections.count; i++) {
        total += [sections[i] floatValue];
    }
    CGPoint center = CGPointMake(kDefaultFrame.origin.x + kDefaultFrame.size.width/2, kDefaultFrame.origin.y + kDefaultFrame.size.height/2);
    for (NSInteger j = 0; j < sections.count; j++) {
        CGFloat scale = [sections[j] floatValue]/total;
        UIColor *color = sectionColors[j];
        endAngle = starAngle + scale * 2 * M_PI;
        
        UIBezierPath *fanshapedPath = [UIBezierPath bezierPathWithArcCenter:center radius:(kDefaultFrame.size.height - 10)/2 startAngle:starAngle endAngle:endAngle clockwise:YES];
        [fanshapedPath addLineToPoint:center];
        [fanshapedPath closePath];
        CAShapeLayer *fanshapedLayer = [CAShapeLayer layer];
        fanshapedLayer.path = fanshapedPath.CGPath;
        fanshapedLayer.fillColor = color.CGColor;
        
        starAngle = endAngle;
        [layer addSublayer:fanshapedLayer];
    }
    [self.view.layer addSublayer:layer];
}
/** 时光网APP 购票layer */
- (void)draw
{
    CGSize finalSize = CGSizeMake(self.view.frame.size.width, 300);
    CGFloat layerHeight = finalSize.height/2;
    CAShapeLayer *layer = [CAShapeLayer layer];
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(0, finalSize.height - layerHeight)];
    [path addLineToPoint:CGPointMake(0, finalSize.height -1)];
    [path addLineToPoint:CGPointMake(finalSize.width, finalSize.height-1)];
    [path addLineToPoint:CGPointMake(finalSize.width, layerHeight)];
    [path addQuadCurveToPoint:CGPointMake(0, layerHeight) controlPoint:CGPointMake(self.view.frame.size.width/2, layerHeight - 40)];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor blackColor].CGColor;
    [self.view.layer addSublayer:layer];
}
// 动画画圆
- (void)drawRoundWithAnimation
{
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    baseAnimation.fromValue = @0.0;
    baseAnimation.toValue = @1.0;
    baseAnimation.duration = 2.0;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:kDefaultFrame cornerRadius:kDefaultFrame.size.height/2];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.lineWidth = 5;
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.strokeColor = [UIColor blackColor].CGColor;
    [layer addAnimation:baseAnimation forKey:nil];
    [self.view.layer addSublayer:layer];
}
//动画绘制曲线 线边宽
- (void)drawCurveWithAnimation3
{
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    //* strokeEnd取值范围 0-1 */
    //* 初始状态 */
    baseAnimation.fromValue = @0.0;
    //* 最终 */
    baseAnimation.toValue = @1.0;
    baseAnimation.duration = 2.0f;
    
    CABasicAnimation *baseAnimation2 = [CABasicAnimation animationWithKeyPath:@"lineWidth"];
    baseAnimation2.fromValue = @1.0;
    baseAnimation2.toValue = @10.0;
    baseAnimation2.duration = 2.0f;
    
    CGPoint starPoint = CGPointMake(50, 300);
    CGPoint endPoint = CGPointMake(self.view.frame.size.width - 50, 300);
    CGPoint controlPoint = CGPointMake(starPoint.x+(endPoint.x - starPoint.x)*0.7, 200);
    CGPoint controlPoint2 = CGPointMake(starPoint.x+(endPoint.x - starPoint.x)*0.3, 400);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    CAShapeLayer *layer = [CAShapeLayer layer];
    [path moveToPoint:starPoint];
    
    [path addCurveToPoint:endPoint controlPoint1:controlPoint controlPoint2:controlPoint2];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor blackColor].CGColor;
    [layer addAnimation:baseAnimation forKey:nil];
    [layer addAnimation:baseAnimation2 forKey:nil];
    [self.view.layer addSublayer:layer];
}
//中间向两边画
- (void)drawFromMiddleToBorder
{
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
    baseAnimation.fromValue = @0.5;
    baseAnimation.toValue = @0.0;
    baseAnimation.duration = 2.0f;
    
    CABasicAnimation *baseAnimation2 = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    baseAnimation2.fromValue = @0.5;
    baseAnimation2.toValue = @1.0;
    baseAnimation2.duration = 2.0f;
    
    CGPoint starPoint = CGPointMake(50, 300);
    CGPoint endPoint = CGPointMake(self.view.frame.size.width - 50, 300);
    CGPoint controlPoint = CGPointMake(starPoint.x+(endPoint.x - starPoint.x)*0.7, 200);
    CGPoint controlPoint2 = CGPointMake(starPoint.x+(endPoint.x - starPoint.x)*0.3, 400);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    CAShapeLayer *layer = [CAShapeLayer layer];
    [path moveToPoint:starPoint];
    
    [path addCurveToPoint:endPoint controlPoint1:controlPoint controlPoint2:controlPoint2];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor blackColor].CGColor;
    [layer addAnimation:baseAnimation2 forKey:nil];
    [layer addAnimation:baseAnimation forKey:nil];
    [self.view.layer addSublayer:layer];
}
//动画绘制曲线
- (void)drawCurveWithAnimation
{
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];;
    baseAnimation.fromValue = @0.0;
    baseAnimation.toValue = @1.0;
    baseAnimation.duration = 2;
    
    CGPoint starPoint = CGPointMake(50, 300);
    CGPoint endPoint = CGPointMake(self.view.frame.size.width - 50, 300);
    CGPoint controlPoint = CGPointMake(starPoint.x+(endPoint.x - starPoint.x)*0.7, 200);
    CGPoint controlPoint2 = CGPointMake(starPoint.x+(endPoint.x - starPoint.x)*0.3, 400);
    UIBezierPath *path = [UIBezierPath bezierPath];
    CAShapeLayer *layer = [CAShapeLayer layer];
    [path moveToPoint:starPoint];
    
    [path addCurveToPoint:endPoint controlPoint1:controlPoint2 controlPoint2:controlPoint];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor blackColor].CGColor;
    [layer addAnimation:baseAnimation forKey:nil];
    [self.view.layer addSublayer:layer];
}
//S型曲线
- (void)drawSshapeCurve
{
    CGPoint starPoint = CGPointMake(50, 300);
    CGPoint endPoint = CGPointMake(self.view.frame.size.width -50, 300);
    CGPoint controlPoint = CGPointMake(starPoint.x+(endPoint.x - starPoint.x)*0.7, 200);
    CGPoint controlPoint2 = CGPointMake(starPoint.x+(endPoint.x - starPoint.x)*0.3, 400);
    CALayer *layer1 = [CALayer layer];
    layer1.frame = CGRectMake(starPoint.x-2.5, starPoint.y-2.5, 5, 5);
    layer1.backgroundColor = [UIColor redColor].CGColor;
    
    CALayer *layer2 = [CALayer layer];
    layer2.frame = CGRectMake(controlPoint.x-2.5, controlPoint.y-2.5, 5, 5);
    layer2.backgroundColor = [UIColor redColor].CGColor;
    
    CALayer *layer3 = [CALayer layer];
    layer3.frame = CGRectMake(endPoint.x-2.5, endPoint.y-2.5, 5, 5);
    layer3.backgroundColor = [UIColor redColor].CGColor;
    
    CALayer *layer4 = [CALayer layer];
    layer4.frame = CGRectMake(controlPoint2.x-2.5, controlPoint2.y-2.5, 5, 5);
    layer4.backgroundColor = [UIColor redColor].CGColor;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    CAShapeLayer *layer = [CAShapeLayer layer];
    [path moveToPoint:starPoint];
    [path addCurveToPoint:endPoint controlPoint1:controlPoint controlPoint2:controlPoint2];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor blackColor].CGColor;
    
    [self.view.layer addSublayer:layer1];
    [self.view.layer addSublayer:layer2];
    [self.view.layer addSublayer:layer3];
    [self.view.layer addSublayer:layer4];
    [self.view.layer addSublayer:layer];
}
//实心矩形
- (void)drawSolidRectangular
{
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = kDefaultFrame;
    layer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:layer];
}

//空心矩形
- (void)drawHollowRectangular
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:kDefaultFrame];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.strokeColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:layer];
}
//圆角图形
- (void)drawRoundedCornerRadiusShape
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:kDefaultFrame cornerRadius:20];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.strokeColor = [UIColor blackColor].CGColor;
    [self.view.layer addSublayer:layer];
}

//扇形
- (void)drawFanshaped
{
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:100 startAngle:M_PI_4 - M_PI endAngle:-M_PI_4 clockwise:YES];
    CGPoint centerPoint = self.view.center;
    [path addLineToPoint:centerPoint];
    [path closePath];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.strokeColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:layer];
}

//圆
- (void)drawRound
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:kDefaultFrame cornerRadius:kDefaultFrame.size.width/2];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.strokeColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:layer];
}

//右上圆角
- (void)drawByRoundingCornersWithTopRight
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:kDefaultFrame byRoundingCorners:UIRectCornerTopRight cornerRadii:kDefaultFrame.size];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.strokeColor = [UIColor redColor].CGColor;
    layer.fillColor = [UIColor whiteColor].CGColor;
    [self.view.layer addSublayer:layer];
}
//椭圆
- (void)drawOval
{
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake((self.view.frame.size.width - 200)/2, 100, 200, 100)];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.strokeColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:layer];
}
//曲线
- (void)drawCurve
{
    CGPoint starPoint = CGPointMake(50, 300);
    CGPoint endPoint = CGPointMake(self.view.frame.size.width -50, 300);
    CGPoint controlPoint = CGPointMake(starPoint.x+(endPoint.x - starPoint.x)*0.6, 200);
    CALayer *layer1 = [CALayer layer];
    layer1.frame = CGRectMake(starPoint.x-2.5, starPoint.y-2.5, 5, 5);
    layer1.backgroundColor = [UIColor redColor].CGColor;
    
    CALayer *layer2 = [CALayer layer];
    layer2.frame = CGRectMake(controlPoint.x-2.5, controlPoint.y-2.5, 5, 5);
    layer2.backgroundColor = [UIColor redColor].CGColor;
    
    CALayer *layer3 = [CALayer layer];
    layer3.frame = CGRectMake(endPoint.x-2.5, endPoint.y-2.5, 5, 5);
    layer3.backgroundColor = [UIColor redColor].CGColor;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    CAShapeLayer *layer = [CAShapeLayer layer];
    [path moveToPoint:starPoint];
    [path addQuadCurveToPoint:endPoint controlPoint:controlPoint];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor blackColor].CGColor;
    
    [self.view.layer addSublayer:layer1];
    [self.view.layer addSublayer:layer2];
    [self.view.layer addSublayer:layer3];
    [self.view.layer addSublayer:layer];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
